package com.ssyt.store.sys.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.ssyt.store.common.BaseDao;
import com.ssyt.store.common.EasyUIPageModel;
import com.ssyt.store.common.util.MD5Util;
import com.ssyt.store.common.util.StringUtil;
import com.ssyt.store.sys.dao.SysUserDao;
import com.ssyt.store.sys.entity.SysUserEntity;

//@Transactional
@Repository
public class SysUserDaoImpl extends BaseDao<SysUserEntity, String> implements
		SysUserDao {

	/**
	 * 目前只做部分验证
	 */
	@Override
	public SysUserEntity login(String loginCode, String loginPassword) {
		String hql = "SELECT new SysUserEntity(u.id, u.code, u.password, u.name, u.state, u.registerTime, r.id, r.name) FROM SysUserEntity u, SysUserRoleEntity ur, SysRoleEntity r WHERE u.id = ur.sysUserId AND ur.sysRoleId = r.id AND u.state = 1 AND code = ? AND password = ? ";
		return super.uniqueEntityByHql(hql, loginCode, loginPassword);
	}

	@Override
	public String addUser(SysUserEntity user) {
		String password = user.getPassword();
		if(!StringUtil.isNotEmpty(password)){
			user.setPassword(user.getCode());
		}
		user.setRegisterTime(new java.util.Date());
		return save(user);
	}

	@Override
	public EasyUIPageModel<SysUserEntity> listUser(String sort, String order,
			int page, int rows) {
		String hql = "SELECT new SysUserEntity(u.id, u.code, u.name, u.state, u.registerTime, r.id, r.name) FROM SysUserEntity u, SysUserRoleEntity ur, SysRoleEntity r WHERE u.id = ur.sysUserId AND ur.sysRoleId = r.id AND u.state = 1";
		return pageEntityByHqlEasyUI(page, rows, hql);
	}

	@Override
	public SysUserEntity showUserInfo(String id) {
		String hql = "SELECT new SysUserEntity(u.id, u.code, u.name, u.state, u.registerTime, r.id, r.name) FROM SysUserEntity u, SysUserRoleEntity ur, SysRoleEntity r WHERE u.id = ur.sysUserId AND ur.sysRoleId = r.id AND u.id = ?";
		return uniqueEntityByHql(hql, id);
	}

	@Override
	public String updateUser(SysUserEntity user) {
		SysUserEntity oldUser = get(user.getId());
		if (oldUser != null) {
			oldUser.setName(user.getName());
			oldUser.setState(user.getState());
		}
		return save(oldUser);
	}

	@Override
	public Integer deleteUser(String ids) {
		String hql = "UPDATE SysUserEntity SET state = 2 WHERE id IN (:ids)";
		Integer row = getSession().createQuery(hql)
				.setParameterList("ids", ids.split(",")).executeUpdate();
		return row;
	}

	// FIXME 待优化
	@Override
	public EasyUIPageModel<SysUserEntity> listUserBySearch(String sort,
			String order, int page, int rows, String searchName, String roleId,
			Integer state) {
		List<Object> paramList = new ArrayList<>();
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT new SysUserEntity(u.id, u.code, u.name, u.state, u.registerTime, r.id, r.name) ");
		sb.append("FROM SysUserEntity u, SysUserRoleEntity ur, SysRoleEntity r ");
		sb.append("WHERE u.id = ur.sysUserId AND ur.sysRoleId = r.id ");
		if (state != -1) {
			sb.append("AND u.state = ? ");
			paramList.add(state);
		}
		if (StringUtils.isNotBlank(searchName)) {
			sb.append("AND u.name LIKE ? ");
			paramList.add("%" + searchName + "%");
		}
		if (StringUtils.isNotBlank(roleId) && !"0".equals(roleId)
				&& !"所有角色".equals(roleId) && !"sr-000".equals(roleId)) {
			sb.append("AND r.id = ? ");
			paramList.add(roleId);
		}
		return pageEntityByHqlEasyUI(page, rows, sb.toString(), paramList);
	}

	@Override
	public Integer updateUserState(SysUserEntity user) {
		SysUserEntity oldUser = get(user.getId());
		oldUser.setState(3 - user.getState());
		update(oldUser);
		return 1;
	}

	public SysUserEntity showUserInfoPassword(String id) {
		String hql = "SELECT new SysUserEntity(u.password) FROM SysUserEntity u, SysUserRoleEntity ur, SysRoleEntity r WHERE u.id = ur.sysUserId AND ur.sysRoleId = r.id AND u.id = ?";
		return uniqueEntityByHql(hql, id);
	}

	@Override
	public String equalPassword(String password, String id) {
		SysUserEntity showUserInfo = showUserInfoPassword(id);
		String psd = showUserInfo.getPassword();
		if (psd.endsWith(MD5Util.md5(password))) {
			return "equal";
		}
		return "";
	}

	@Override
	public void updatePassword(String newPassword, String id) {
		SysUserEntity newUser = get(id);
		System.out.println(newUser);
		if (newUser != null) {
			newUser.setPassword(newPassword);
		}
		update(newUser);
	}

}
